package citibank;

import java.io.FileReader;
import java.util.Random;

import com.alignet.bean.VPOS20Bean;
import com.alignet.plugin.Receive20;
import com.alignet.plugin.Send20;

public class CitiBankTest {
	/*
    static {
        ProtocolSocketFactory easy = new EasySSLProtocolSocketFactory();
        Protocol protocol = new Protocol("https", easy, 443);
        Protocol.registerProtocol("https", protocol);
    }
    */
	
	private static final String CHAR_POOL = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

    public static void main(String[] args) throws Exception {
    	send();
    	//receive();
    }
    
    public static void receive()throws Exception{
    	
    	String xml="xx";
    	String skey="xx";
    	String sign="xx";
    	
    	VPOS20Bean bean = new VPOS20Bean();
    	bean.setCipheredXML(xml);
    	bean.setCipheredSessionKey(skey);
    	bean.setCipheredSignature(sign);
    	
    	Receive20 receive = new Receive20(
    			
    			
    			new FileReader("D:\\Remote_Gateways\\Alignet_CitiBank\\Java\\llaves\\ALIGNET.TESTING.NOPHP.SIGNATURE.PUBLIC.txt"),
    			
    			new FileReader("D:\\Remote_Gateways\\Alignet_CitiBank\\Java\\keys\\encryption_private.key"),
    			 						  
    									  "D412589745DF36FA");
    	
    	receive.execute(bean);
    	
    	//Verify the digital signature, which will verify the data integrity
    	out("isValidSign="+bean.isValidSign());
    	out("getAuthorizationResult="+bean.getAuthorizationResult());
    	out("getAuthorizationCode="+bean.getAuthorizationCode());
    	out("getErrorCode="+bean.getErrorCode());
    	out("getErrorMessage="+bean.getErrorMessage());
    	
    	out("getAcquirerId="+bean.getAcquirerId());
    	out("getCommerceId="+bean.getCommerceId());
    	
    	out("getReserved1="+bean.getReserved1());
    }
    
    public static void send()throws Exception {
    	VPOS20Bean bean = new VPOS20Bean();
    	bean.setAcquirerId("122");
    	bean.setCommerceId("5618");
    	bean.setPurchaseOperationNumber(generateRandom(10));
    	bean.setPurchaseAmount("10030");
    	//bean.setLanguage("ZH");
    	bean.setPurchaseCurrencyCode("840");//USD
    	//bean.setPurchaseCurrencyCode("590");//Balboa
    	
    	
    	bean.setBillingFirstName("fn");
    	bean.setBillingLastName("ln");
    	bean.setBillingEMail("a@b.com");
    	bean.setBillingAddress("First ring road");
    	bean.setBillingZIP("10004");
    	bean.setBillingCity("New York");
    	bean.setBillingState("NY");
    	bean.setBillingCountry("US");
    	bean.setBillingPhone("123456789");
    	
    	bean.setReserved1("vid-vid");
    	
    	/*
    	bean.setShippingFirstName("fn");
    	bean.setShippingLastName("ln");
    	bean.setShippingEMail("a@b.com");
    	bean.setShippingAddress("First ring road");
    	bean.setShippingZIP("10004");
    	bean.setShippingCity("New York");
    	bean.setShippingState("New York");
    	bean.setShippingCountry("US");
    	bean.setShippingPhone("123456789");
    	*/
    	                        //Public Encoded Key
    	Send20 send=new Send20(new FileReader("D:\\Remote_Gateways\\Alignet_CitiBank\\Java\\llaves\\ALIGNET.TESTING.NOPHP.CRYPTO.PUBLIC.txt"),
    			                //Private Signature Key
    							new FileReader("D:\\Remote_Gateways\\Alignet_CitiBank\\Java\\keys\\signing_private.key"),"D412589745DF36FA");
    	
    	send.execute(bean);
    	
    	out("XML:"+bean.getCipheredXML());
    	out("");
    	out("Signature:"+bean.getCipheredSignature());
    	out("");
    	out("SessionKey:"+bean.getCipheredSessionKey());

    	
    	
    	//Receive20 r;
    }
    
    public static void out(Object obj){
    	if(obj!=null){
    		System.out.println(obj.toString());
    	}else{
    		System.out.println(obj);
    	}
    }
    
    private static String generateRandom(int length){
        Random rand = new Random();
        char[] passwordChar = new char[length];
        for (int i = 0; i < length; i++) {
                passwordChar[i] = CHAR_POOL.charAt(rand.nextInt(CHAR_POOL.length()));
        }
        
        return new String(passwordChar);
    }
}
